package cn.team.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.team.entity.User;
import cn.team.service.UserService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user")
@RestController
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("/register")
    public Map<String, String> register(HttpServletResponse response,
        @RequestParam("username")String username,@RequestParam("password")String password) {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        HashMap<String, String> map = new HashMap<>();
        User user = userService.getUser(username);
        if (user != null) {
            map.put("msg","此账号已经注册");
            return map;
        }
        int result = userService.saveUser(username, password,FileController.getDefaultPortrait());
        System.out.println("用户保存---"+result);
        map.put("msg","ok");
        return map;
    }

    @GetMapping("/user/{username}")
    public HashMap<String, String> user(@PathVariable("username")String username, HttpServletResponse response) {
        User user = userService.getUser(username);
        HashMap<String,String> map = new HashMap();
        if (user!=null) {
            map.put("msg","ok");
        } else {
            map.put("msg", "err");
        }
        return map;
    }

    @PostMapping
    public HashMap<String, String> login(@RequestParam("username")String username,@RequestParam("password") String password) {
        User user = userService.getUser(username);
        HashMap<String, String> map = new HashMap<>();
        map.put("msg", "ok");
        return map;
    }
}
